package com.ld.basic.service.impl;

import com.ld.basic.mapper.BaseMapper;
import com.ld.basic.query.BaseQuery;
import com.ld.basic.service.IBaseService;
import com.ld.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public T loadById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public List<T> loadAll() {
        return mapper.selectAll();
    }

    @Override
    @Transactional
    public void insert(T t) {
        mapper.add(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
      mapper.delete(id);
    }

    @Override
    @Transactional
    public void update(T t) {
      mapper.update(t);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
      mapper.batchDelete(ids);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Long total = mapper.queryTotal(query);
        if (total>0){
            List<T> queryList = mapper.queryList(query);
            return new PageList<>(total,queryList);
        }
        return new PageList<>();
    }
}
